<!--
 * @Author: Bennty
 * @Date: 2024-11-22 09:42:24
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-11-27 13:36:22
 * @Description: 请填写简介 
-->
<template>
    <div class="p-2">
        <transition
            :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave"
        >
            <div class="search">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                    <el-form-item label="工序名称" prop="workName">
                        <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <el-row :gutter="8" class="mb8">
                    <el-col class="header-button-container" :span="1.5">
                        <el-button type="success" plain icon="Plus" @click="handleClickAdd">新增</el-button>
                    </el-col>
                </el-row>
            </template>
            <el-table :data="processList">
                <el-table-column label="工序名称" prop="workName" align="center"></el-table-column>
                <el-table-column label="单位" prop="unit" align="center"></el-table-column>
                <el-table-column label="计价方式" align="center">
                    <template #default="scope">
                        <span class="not-alert">{{ `￥ ${scope.row.price}/${scope.row.unit}` }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="预计工期" align="center">
                    <template #default="scope">
                        <span class="not-alert">{{ `${scope.row.constructionPeriod} 天` }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="关联外协厂" prop="supplierNum" align="center">
                </el-table-column>
                <el-table-column label="外协厂详情" align="center">
                    <template #default="scope">
                        <el-button type="primary" link @click="handleClickCheckFactories(scope.row)" >查看详情</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-tooltip content="编辑" placement="top">
                            <el-button link type="primary" :style="{fontSize:'15px'}" @click="handleClickEdit(scope.row)" icon="Edit"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link type="danger" :style="{fontSize:'15px'}" @click="handleDeleteProcess(scope.row)"  icon="Delete"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                @pagination="getProcesses" />
        </el-card>
        <addProcess :visible="addProcessVisible" :formModel="form" :factorys="factorys" @close="handleCloseAdd" @submit="handleSubmit"/>
        <editProcess :visible="editProcessVisible" :formModel="processDetail" :factorys="factorys" @close="handleCloseEdit" @submit="handleSubmit"  />
        <factoryDetail :visible="checkFactoryDetailVisible" :formModel="processDetail" @close="handleCloseCheckFactories" @submit="handleSubmit" />
    </div>
</template>

<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { Form,QueryProcessType, ProcessVO } from '@/api/sj/purchaseManagement/processManagement/types';
import { getSuppliersApi } from '@/api/sj/purchaseManagement/supplierManagement/index';
import { getProcessListApi, getProcessDetailApi, deleteProcessApi } from '@/api/sj/purchaseManagement/processManagement';
import addProcess from './components/addProcess.vue';
import editProcess from './components/editProcess.vue';
import factoryDetail from './components/factoryDetail.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<FormInstance>();
const data = reactive<pageDataExcludeRuses<Form, QueryProcessType>>({
    form:{
        id: undefined,
        workName: undefined,
        unit: undefined,
        price: undefined,
        constructionPeriod: undefined,
        supplyJoinWorkProcedures: [],
        workStatus: 1
    },
    queryParams:{
        pageNum: 1,
        pageSize: 30,
        workName: undefined
    }
});

const { form, queryParams } = toRefs(data);
const loading = ref(false);
const processList = ref<ProcessVO[]>([]);
const totalData = ref(0);
const factorys = ref<any[]>([]);

const addProcessVisible = ref(false);
const processDetail = ref<any>({});
const editProcessVisible = ref(false);
const checkFactoryDetailVisible = ref(false);

const getProcesses = async () => {
    loading.value = true;
    const {code, rows, total} = await getProcessListApi(queryParams.value);
    if(code == 200){
        processList.value = rows;
        totalData.value = total;
        loading.value = false;
    };
};

const getFactorys = async() => {
    const { code, data } = await getSuppliersApi('2');
    if(code == 200){
        factorys.value = data;
    }
};

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getProcesses();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};

const handleSubmit = (result: boolean) => {
    if(result){
        proxy?.$modal.msgSuccess('成功');
        getProcesses();
    }
};

const handleClickAdd = () => {
    addProcessVisible.value = true;
};

const handleCloseAdd = () => {
    addProcessVisible.value = false;
};

const handleClickEdit = async (row: ProcessVO) => {
    const {code, data} = await getProcessDetailApi(row.id);
    if(code == 200){
        processDetail.value = data;
        editProcessVisible.value = true;
    };
};

const handleCloseEdit = () => {
    editProcessVisible.value = false;
};

const handleDeleteProcess = async (row: ProcessVO) => {
    await proxy?.$modal.confirm('确认要删除工序？')
    const { code } = await deleteProcessApi(row.id);
    if(code == 200){
        proxy?.$modal.msgSuccess('删除工序成功');
        getProcesses();
    };
};

const handleClickCheckFactories = async (row: ProcessVO) => {
    const {code, data} = await getProcessDetailApi(row.id);
    if(code == 200){
        processDetail.value = data;
        checkFactoryDetailVisible.value = true;
    };
};

const handleCloseCheckFactories = () => {
    checkFactoryDetailVisible.value = false;
};

onMounted(()=>{
    getProcesses();
    getFactorys();
})

</script>

<style scoped>

</style>